package com.loveprogrammer.eternity.factory;

import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.loveprogrammer.eternity.action.BaseAction;
import com.loveprogrammer.eternity.enums.QualityEnum;
import com.loveprogrammer.eternity.factory.template.CharacterTemplate;
import com.loveprogrammer.eternity.factory.template.HeroTemplate;
import com.loveprogrammer.eternity.factory.template.JobTemplate;
import com.loveprogrammer.eternity.model.Hero;
import com.loveprogrammer.eternity.model.Monster;
import com.loveprogrammer.eternity.model.game.HeroWrapper;
import com.loveprogrammer.eternity.model.game.PetWrapper;
import com.loveprogrammer.eternity.model.product.Equipment;
import com.loveprogrammer.eternity.skill.Skill;
import com.loveprogrammer.eternity.utils.ScannerInput;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * @version 1.0.0
 * @description: 英雄工厂
 * @author: eric
 * @date: 2022-08-09 19:53
 **/
public class JobFactory extends BaseAction {

    private static List<JobTemplate> templates;

    private static Map<Integer,JobTemplate> jobMap;

    static {
        try {
            // 读取配置文件，然后加载到weaponTemplates中
            ClassLoader classLoader = EquipmentFactory.class.getClassLoader();
            InputStream inputStream = classLoader.getResourceAsStream("job.json");

            assert inputStream != null;
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            StringBuilder responseStrBuilder = new StringBuilder();

            String inputStr;
            while ((inputStr = streamReader.readLine()) != null)
                responseStrBuilder.append(inputStr);

            templates = JSONArray.parseArray(responseStrBuilder.toString(),JobTemplate.class);

            jobMap = templates.stream().collect(Collectors.toMap(CharacterTemplate::getId, e ->e));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static List<JobTemplate> getTemplates(){
        return templates;
    }


    public static void transfer(HeroWrapper heroWrapper) throws IOException, ClassNotFoundException {
        // 查看是否已经到了可以转职的情况
        Hero hero = heroWrapper.getHero();
        HeroTemplate template = hero.getTemplate();
        int highestLevel = template.getHighestLevel();
        if(hero.getLevel() >= highestLevel){
            // 查看能转职的
            String nextLevelJobList = template.getNextLevelJobList();
            if(!StringUtils.isEmpty(nextLevelJobList)) {
                String[] nextJobIds = nextLevelJobList.split(",");
                eventManager.notifyMessage("目前可以转职的职业有：", 1);
                Map<Integer,JobTemplate> chooseMap = new HashMap<>();
                for (int i = 1; i < nextJobIds.length+1; i++) {
                    JobTemplate jobTemplate = jobMap.get(Integer.parseInt(nextJobIds[i-1]));
                    chooseMap.put(i,jobTemplate);
                    eventManager.notifyMessage( i + ":" + jobTemplate.getName() + ",说明：" + jobTemplate.getDesc() , 1);
                }
                int money = hero.getLevel() * hero.getLevel() * hero.getLevel() * 20;
                eventManager.notifyPrompt("转职需要金币" + money + ",请选择(0放弃)： ");
                int goldCoin = heroWrapper.getWarehouse().getGoldCoin();
                if(goldCoin < money) {
                    eventManager.notifyMessage("冒险者，钱不够哦，先去打怪卖装备吧", 1);
                    return;
                }
                int choose = ScannerInput.inputInt(1, nextJobIds.length+1, 0);
                if(choose != 0){
                    //
                    eventManager.notifyMessage("开始转职：", 1);
                    doTransfer(hero,chooseMap.get(choose));
                    heroWrapper.setAliasName(chooseMap.get(choose).getName());
                    eventManager.notifyMessage("转职成功：", 1);
                    eventManager.notifyMessage(hero.prettyPrint(), 1 );
                    heroWrapper.getWarehouse().setGoldCoin(goldCoin - money);
                    heroWrapper.getGameStatus().setJobTransferTimes(heroWrapper.getGameStatus().getJobTransferTimes() + 1);
                }
            }
            else{
                eventManager.notifyMessage("没有找到可以转职的职业", 1);
            }

        }else{
            eventManager.notifyMessage("没有到达可转职的等级，请继续升级，当前等级【" + hero.getLevel() + "】,下次转职的等级为【" + highestLevel + "】级", 1);
        }
    }

    private static void doTransfer(Hero hero, JobTemplate jobTemplate) throws IOException, ClassNotFoundException {
        hero.transfer(jobTemplate);
    }
}
